package com.hksj.sglm.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.base.service.DefaultDeviceService;
import com.hksj.cbm.service.DeviceInstanceService;
import com.hksj.cbm.util.EasyExcelUtil;
import com.hksj.sglm.mapper.SpecialGasPressureMapper;
import com.hksj.sglm.query.SpecialGasPressureHistoryQuery;
import com.hksj.sglm.query.SpecialGasPressureQuery;
import com.hksj.sglm.vo.ExportSpecialGasPressureVO;
import com.hksj.sglm.vo.SpecialGasPressureHistoryVO;
import com.hksj.sglm.vo.SpecialGasPressureVO;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SpecialGasPressureService {

    private static final Logger log = org.slf4j.LoggerFactory.getLogger(SpecialGasPressureService.class);

    @Autowired
    private SpecialGasPressureMapper specialGasPressureMapper;

    @Autowired
    private DefaultDeviceService defaultDeviceService;

    // 查询气体压力监测设备列表
    public IPage<SpecialGasPressureVO> querySpecialGasPressureByPage(SpecialGasPressureQuery query) {
        //声明
        IPage<SpecialGasPressureVO> page = new Page<>(query.getPageNum(), query.getPageSize());

        List<SpecialGasPressureVO> list = specialGasPressureMapper.querySpecialGasPressureByPage(page, query);

        page.setRecords(list);

        return page;
    }

    // 查询气体压力监测设备压力趋势(分页)
    public IPage<SpecialGasPressureHistoryVO> querySpecialGasPressureHistory(SpecialGasPressureHistoryQuery query){
        IPage<SpecialGasPressureHistoryVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        String tableName = defaultDeviceService.getTableName(query.getProductId());
        List<SpecialGasPressureHistoryVO> list = new ArrayList<>();
        Long total = 0L;
        try {
            if(query.getPageNum()>0) {
                query.setPageNum((query.getPageNum() - 1)*query.getPageSize());
            }
            list = specialGasPressureMapper.querySpecialGasPressureHistory(query, tableName);
            total = specialGasPressureMapper.querySpecialGasHistoryCount(query, tableName);
        }catch (Exception e){
            log.error("Error querying table {}: {}", tableName, e.getMessage(), e);
        }
        page.setRecords(list);
        page.setTotal(total);
        return page;
    }



    //导出气体压力监测设备压力趋势
    public void exportSpecialGasPressureHistory(SpecialGasPressureHistoryQuery query, HttpServletResponse response){
        List<SpecialGasPressureHistoryVO> list = new ArrayList<>();
        String tableName = defaultDeviceService.getTableName(query.getProductId());
        try {
            list = specialGasPressureMapper.querySpecialGasPressureHistory(query, tableName);
        }catch (Exception e){
            log.error("Error querying table {}: {}", tableName, e.getMessage(), e);
        }

        List<ExportSpecialGasPressureVO> exportList = new ArrayList<>();

        if (!CollectionUtils.isEmpty(list)){
            int i = 1;
            for (SpecialGasPressureHistoryVO vo : list){
                ExportSpecialGasPressureVO exportVO = new ExportSpecialGasPressureVO();
                exportVO.setSeq(String.valueOf(i++));
                exportVO.setReportingTime(vo.getReportingTime());
                exportVO.setValue(vo.getValue());
                exportList.add(exportVO);
            }
        }

        String fileName = new StringBuffer()
                .append("气体压力监测设备压力趋势")
                .append(new SimpleDateFormat("_yyyyMMddHHmmss").format(new Date()))
                .toString();

        EasyExcelUtil.exportExcelData(response, fileName, exportList, ExportSpecialGasPressureVO.class);

    }

}
